using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.USDK;
using static IQIGame.Onigao.GamePlay.UISettingTabType;

namespace IQIGame.Onigao.GamePlay
{
    public class UISetting : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UISetting
        {
            #region ObjectBinding Generate
            public UnityEngine.GameObject toggleGroups { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton button_Logout { protected set; get; }
            public UnityEngine.GameObject setting_Other { protected set; get; }
            public UnityEngine.GameObject setting_Audio { protected set; get; }
            public UnityEngine.GameObject setting_Inform { protected set; get; }
            public UnityEngine.GameObject setting_Game { protected set; get; }
            public UnityEngine.GameObject topLeftBack { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleGroups", out var __tbv0);
                this.toggleGroups = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("button_Logout", out var __tbv1);
                this.button_Logout = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("setting_Other", out var __tbv2);
                this.setting_Other = __tbv2;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("setting_Audio", out var __tbv3);
                this.setting_Audio = __tbv3;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("setting_Inform", out var __tbv4);
                this.setting_Inform = __tbv4;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("setting_Game", out var __tbv5);
                this.setting_Game = __tbv5;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("topLeftBack", out var __tbv6);
                this.topLeftBack = __tbv6;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UISetting),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields
        private IrregularListAdapter<UIN_CommonToggleOn> settingTabs;
        private UIN_SettingGame settingGame;
        private UIN_SettingAudio settingAudio;
        private UIBaseNode currentNode;

        private int indexSelectedTab = -1;
        #endregion fields

        #region properties

        protected UIB_UISetting ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UISetting();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this.settingTabs = this.InitIrregularList<UIN_CommonToggleOn>(this.ui.toggleGroups, this.OnTabChanges);
            this.settingTabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTabSelected);

            this.settingGame = this.InitChildNode<UIN_SettingGame>(this.ui.setting_Game);
            this.settingAudio = this.InitChildNode<UIN_SettingAudio>(this.ui.setting_Audio);

            var topLeftBack = this.InitChildNode<UIN_TopLeftBack>(this.ui.topLeftBack);
            topLeftBack.Show();
        }

        protected override void OnShow(UIBaseData data)
        {
            this.ShowView();
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            this.ui.button_Logout.onClick.AddListener(this.OnClickLogout);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds

        private void ShowView()
        {
            this.ShowSettingTabs();
        }

        private void ShowSettingTabs()
        {
            int cnt = (int)MaxLength;
            this.settingTabs.RefillCells(cnt, 0);
        }

        private void OnTabChanges(UIN_CommonToggleOn item, int index)
        {
            item.Show(index);
        }

        private void OnTabSelected(int index, UIN_CommonToggleOn item)
        {
            ShowCurrent(index);
        }

        private void ShowCurrent(int index)
        {
            Save(this.indexSelectedTab);

            currentNode?.Hide();
            currentNode = null;
            this.indexSelectedTab = index;
            switch ((UISettingTabType)index)
            {
                case SettingAudio:
                    currentNode = settingAudio;
                    settingAudio.Show(index);
                    break;
                case SettingNotice:
                    break;
                case SettingOther:
                    break;
                default:
                    currentNode = settingGame;
                    settingGame.Show(index);
                    break;
            }
        }

        private void Save(int index)
        {
            if (index < 0)
                return;

            switch ((UISettingTabType)index)
            {
                case SettingAudio:
                    settingAudio.Save();
                    break;
                case SettingNotice:
                    break;
                case SettingOther:
                    break;
                default:
                    settingGame.Save();
                    break;
            }
        }

        private async void OnClickLogout()
        {
            Save(this.indexSelectedTab);
            if (ManagerCenter.ProjectSetting.IsSDKMode())
            {
                IQIUSDK.Instance.logout();
            }
            else
            {
                await GameServerModule.NetCore.CS_logout();
                ProcedureFSM.Instance.ChangeState<ProcedureReset>();
            }
        }

        public override void Close()
        {
            Save(this.indexSelectedTab);
            base.Close();
        }
        #endregion methonds
    }
}
